package org.dromara.bykt.service.impl;

import org.checkerframework.checker.units.qual.A;
import org.dromara.bykt.domain.BYKTCourse;
import org.dromara.bykt.domain.vo.BYKTCourseVo;
import org.dromara.bykt.mapper.BYKTCourseMapper;
import org.dromara.bykt.service.IBYKTCourseService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.dromara.bykt.domain.bo.BYKTCourseCategoryBo;
import org.dromara.bykt.domain.vo.BYKTCourseCategoryVo;
import org.dromara.bykt.domain.BYKTCourseCategory;
import org.dromara.bykt.mapper.BYKTCourseCategoryMapper;
import org.dromara.bykt.service.IBYKTCourseCategoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 课程分类Service业务层处理
 *
 * @author Hou
 * @date 2025-06-22
 */
@RequiredArgsConstructor
@Service
public class BYKTCourseCategoryServiceImpl implements IBYKTCourseCategoryService {

    private final BYKTCourseCategoryMapper baseMapper;

    private final BYKTCourseMapper courseMapper;

    /**
     * 查询课程分类
     *
     * @param id 主键
     * @return 课程分类
     */
    @Override
    public BYKTCourseCategoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询课程分类列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 课程分类分页列表
     */
    @Override
    public TableDataInfo<BYKTCourseCategoryVo> queryPageList(BYKTCourseCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BYKTCourseCategory> lqw = buildQueryWrapper(bo);
        Page<BYKTCourseCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的课程分类列表
     *
     * @param bo 查询条件
     * @return 课程分类列表
     */
    @Override
    public List<BYKTCourseCategoryVo> queryList(BYKTCourseCategoryBo bo) {
        LambdaQueryWrapper<BYKTCourseCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BYKTCourseCategory> buildQueryWrapper(BYKTCourseCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BYKTCourseCategory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BYKTCourseCategory::getId);
        lqw.like(StringUtils.isNotBlank(bo.getName()), BYKTCourseCategory::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BYKTCourseCategory::getCode, bo.getCode());
        lqw.eq(bo.getSort() != null, BYKTCourseCategory::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BYKTCourseCategory::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增课程分类
     *
     * @param bo 课程分类
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BYKTCourseCategoryBo bo) {
        BYKTCourseCategory add = MapstructUtils.convert(bo, BYKTCourseCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程分类
     *
     * @param bo 课程分类
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BYKTCourseCategoryBo bo) {
        BYKTCourseCategory update = MapstructUtils.convert(bo, BYKTCourseCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BYKTCourseCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除课程分类信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
            List<Long> nonExistingIds = new ArrayList<>(ids);
            for (Long id : nonExistingIds) {
                Long aLong = courseMapper.selectCount(new LambdaQueryWrapper<BYKTCourse>().eq(BYKTCourse::getCategoryId, id));
                if (aLong != 0l) {
                    throw new ServiceException("以下分类ID被课程使用，无法删除: " + id);
                }
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 课程分类列表
     *
     * @return
     */
    @Override
    public List<BYKTCourseCategoryVo> queryCourseCategory() {
        List<BYKTCourseCategoryVo> byktCourseCategoryVos = baseMapper.selectVoList(new LambdaQueryWrapper<>());
        for (BYKTCourseCategoryVo byktCourseCategoryVo : byktCourseCategoryVos) {
            Long id = byktCourseCategoryVo.getId();
            List<BYKTCourseVo> byktCourseVos = courseMapper.selectVoList(new LambdaQueryWrapper<BYKTCourse>().eq(BYKTCourse::getCategoryId, id));
            byktCourseVos.forEach(byktCourseVo -> byktCourseVo.setName(byktCourseVo.getTitle()));
            byktCourseCategoryVo.setChildren(byktCourseVos);
        }
        return byktCourseCategoryVos;
    }
}
